package airport.client.widget.connexion;

import airport.client.Airport;
import airport.client.utils.Constantes;
import airport.shared.beans.UserBean;
import airport.shared.service.AirportService;
import airport.shared.service.AirportServiceAsync;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.*;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;

/**
 * Created by Romain on 20/03/15.
 */
public class ConnexionWB extends Airport {

    FlowPanel form = new FlowPanel();
    Button connexion = new Button("Connexion");
    FlowPanel titre = new FlowPanel("h2");
    TextBox email = new TextBox();
    PasswordTextBox password = new PasswordTextBox();

    public ConnexionWB() {
        initWidget(form);
        checkLogin();
    }

    public void init() {
        form.setStyleName("form-signin");
        titre.getElement().setInnerHTML("Identification");

        email.setStyleName("input-block-level");
        email.getElement().setPropertyString(Constantes.PLACEHOLDER, Constantes.EMAIL);

        email.addKeyDownHandler(new KeyDownHandler() {

            @Override
            public void onKeyDown(KeyDownEvent arg0) {
                if (arg0.getNativeKeyCode() == KeyCodes.KEY_ENTER)
                    connexion.click();
            }
        });
        password.setStyleName("input-block-level");
        password.getElement().setPropertyString(Constantes.PLACEHOLDER, Constantes.PASSWORD);

        password.addKeyDownHandler(new KeyDownHandler() {

            @Override
            public void onKeyDown(KeyDownEvent arg0) {
                if (arg0.getNativeKeyCode() == KeyCodes.KEY_ENTER)
                    connexion.click();
            }
        });

        form.add(titre);
        form.add(email);
        form.add(password);

        connexion.setStyleName("btn btn-primary");

        form.add(connexion);

        email.setFocus(true);
        connexion.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                connexion();
            }
        });
    }

    public void checkLogin() {
        final AirportServiceAsync connexionIface;
        connexionIface = GWT.create(AirportService.class);

        connexionIface.checkLogin(new AsyncCallback<UserBean>() {

            @Override
            public void onFailure(Throwable caught) {
                infoDanger(caught.getMessage());
            }

            @Override
            public void onSuccess(UserBean result) {
                if (result != null) {
                    History.newItem(Constantes.ACCUEILWB);
                } else {
                    init();
                }
            }
        });
    }

    private void connexion() {
        infoDangerHide();
        final AirportServiceAsync connexionIface;
        connexionIface = GWT.create(AirportService.class);

        connexionIface.connexion(email.getText(), password.getText(), new AsyncCallback<UserBean>() {

            @Override
            public void onFailure(Throwable caught) {
                infoDanger(caught.toString());
            }

            @Override
            public void onSuccess(UserBean result) {
                if (result == null)
                    infoDanger("Email / Mot De Passe Incorrect");
                else {
                    infoSuccess("Connexion Réussie");
                    labelTitre.getElement().setInnerHTML("Bienvenue " + result.getPrenom() + " " + result.getNom());
                    initbouton(true);
                    History.newItem(Constantes.ACCUEILWB);
                }
            }
        });
    }

}